/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package library;

import java.util.Calendar;
import java.util.Date;

/**
 * 
 * @author ryan
 */
public class Item {
    private static int nextId = 0;
    
    public enum Type{
        DVD("DVD",2),
        VIDEOTAPE("Video Tape", 3),
        BOOK("Book", 14),
        CHILDRENS_BOOK("Children's Book", 7);
        
        private final int checkoutDuration;
        private final Calendar c;
        private final String name;
        
        
        Type(String name, int days){
            this.checkoutDuration=days;
            this.name=name;
            c=Calendar.getInstance();
        }
        
        public Date getDueDate(Date now){
            c.setTime(now);
            c.add(Calendar.DATE, checkoutDuration);
            return c.getTime();
        }
        
        public Date getCheckoutDate(Date dueDate){
            c.setTime(dueDate);
            c.add(Calendar.DATE, -1*checkoutDuration);
            return c.getTime();
            
        }
        
        @Override
        public String toString(){
            return name;
        }
    }
    
    private final int id;
    private boolean checkedOut;
    private Date dueDate = null;
    private final String title;
    private final String description;
    private final Type t;    
    
    public Item(String title, Type t, String desc){
        this(nextId++, title, t, desc, null);
    }
    
    public Item(int id, String title, Type t, String desc, Date due)
    {
        this.id = id;
        nextId = nextId > id ? nextId : id + 1;
        this.title=title;
        this.t=t;
        this.description=desc;
        this.dueDate = due;
        
        if(due != null)
        {
            checkedOut = true;
        }
    }
    
    /**
    * 
    * @return ID of item
    */
    public int getId(){
        return id;        
    }
    
    /**
     * Return the title of the item
     * @return 
     */
    public String getTitle(){
        return title;
    }
    
    /**
     * return the type of the item(Probably replace String with 
     * @return Type of item(Children's book, DVD, VHS, Book
     */
    public Type getType(){
        return t;        
    }
    
    /**
     * return the details string for the item
     * @return 
     */
    public String getDescription(){
        return description;        
    }
    
    /**
     * get the due date for the item (only valid if checked out)
     * @return 
     */
    public Date getDueDate(){
        if(!checkedOut){
            throw new IllegalStateException("Item not check out");            
        }
        return dueDate;
    }
    
    /**
     * get the checked out status 
     * @return 
     */
    public boolean isCheckedOut(){
        return checkedOut;        
    }
    
    /**
     * Changes the items status to checked out
     */
    public void checkOut(Date checkoutDate){
        this.checkedOut=true;
        this.dueDate= t.getDueDate(checkoutDate);
    }
    
    /**
     * Changes the items status to checked in
     */
    public void checkIn(){
        this.checkedOut=false;
    }    
}